import processing.pdf.*;
Table t;
TreeMap tm;
int rowCount, labelCount;
int count, col;
float percent;
String title;

void setup(){
  size(200, 200, PDF, "out.pdf");  
  //size(620, 480);  
  tm = new TreeMap();
  crunchData(2); 
  displayMapContents(tm);
}

void displayMapContents(TreeMap hm){
  println(title);
  println("------------------------------------");
  Iterator i = hm.entrySet().iterator();  // Get an iterator
  while (i.hasNext()){
    Map.Entry me = (Map.Entry)i.next();
    Integer value = (Integer)me.getValue(); // Retrieve the value as Integer (obj)
    percent = ((float)value/(float)rowCount)*100 ;
    float f = (float)Math.round(percent);
    print(f+"% \t");
    println(me.getKey()+" ["+me.getValue()+"]");
  }
  println("------------------------------------");
  println((rowCount-1)+" Total lines"); // subtract one for the title column
}

void crunchData(int col){
  // t = new Table("Reclass-08-09.tsv");
  // t = new Table("Equities-08-09.tsv");
  // t = new Table("wf-08-09.tsv");
   t = new Table("EMP-apps-08-09.tsv");
  // t = new Table("EMP-Hires-08-09.tsv");
  rowCount = t.getRowCount(  );
  title = t.getString(0, col);

  for (int i = 1; i < rowCount; i++) { // Start at 1 to skip the col title
    String value = t.getString(i, col);

    if(tm.containsKey(value)){
      count = (Integer)tm.get(value);
      tm.put(value,++count);
    }
    else if(!tm.containsKey(value)){
      tm.put(value,1);
    }     
  }  
}

/* 
 Based off of some great example code from Quark
 http://processing.org/discourse/yabb2/YaBB.pl?num=1251333472/9 
 
 treeMap example
 http://www.java2s.com/Code/Java/Collections-Data-Structure/SortingElementsinaTreeMap.htm
 */

